package com.wd.util;

import java.io.File;
import java.lang.reflect.Field;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author long
 * @date 2013-4-28 上午11:28:00
 * @qq 108232706
 * @description SystemUtil
 */
public class SystemUtil {
	private static final Logger logger = LoggerFactory.getLogger(SystemUtil.class);
	public static final boolean isWindows;
	public static  String GAMEHOME="";
	public static  String COMMANDPATH;
	private static List<File> commandJars;
	static {
		isWindows = System.getProperty("os.name").toLowerCase().indexOf("windows") > -1;
		GAMEHOME = System.getProperty("game.home");
//		COMMANDPATH = System.getProperty("game.commandpath");
	}

	public static List<File> getCommandJar() {
		if (commandJars != null)
			return commandJars;
		commandJars = new java.util.ArrayList<File>();
		try {
			if (COMMANDPATH == null)
				throw new Exception("not set -Dgame.commandpath=");
			File[] files = FileUtil.GetFileList(COMMANDPATH);
			String filename;
			for (File file : files) {
				filename = file.getName().toLowerCase();
				// System.out.println(filename);
				if (filename.endsWith(".jar") && filename.indexOf("command") != -1)
					commandJars.add(file);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("", e);
		}

		return commandJars;
	}

	public static String getProperty(String property) {
		return System.getProperty(property);
	}

	public static void printSystemProperty() {
		System.out.println("java.version:" + System.getProperty("java.version"));
		System.out.println("java.vendor:" + System.getProperty("java.vendor"));
		System.out.println("java.vendor.url:" + System.getProperty("java.vendor.url"));
		System.out.println("java.home:" + System.getProperty("java.home"));
		System.out.println("java.vm.specification.version:" + System.getProperty("java.vm.specification.version"));
		System.out.println("java.vm.specification.vendor:" + System.getProperty("java.vm.specification.vendor"));
		System.out.println("java.vm.specification.name:" + System.getProperty("java.vm.specification.name"));
		System.out.println("java.vm.version:" + System.getProperty("java.vm.version"));
		System.out.println("java.vm.vendor:" + System.getProperty("java.vm.vendor"));
		System.out.println("java.vm.name:" + System.getProperty("java.vm.name"));
		System.out.println("java.specification.version:" + System.getProperty("java.specification.version"));
		System.out.println("java.specification.vendor:" + System.getProperty("java.specification.vendor"));
		System.out.println("java.specification.name:" + System.getProperty("java.specification.name"));
		System.out.println("java.class.version:" + System.getProperty("java.class.version"));
		System.out.println("java.class.path:" + System.getProperty("java.class.path"));
		System.out.println("java.library.path:" + System.getProperty("java.library.path"));
		System.out.println("java.io.tmpdir:" + System.getProperty("java.io.tmpdir"));
		System.out.println("java.compiler:" + System.getProperty("java.compiler"));
		System.out.println("java.ext.dirs:" + System.getProperty("java.ext.dirs"));
		System.out.println("os.name:" + System.getProperty("os.name"));
		System.out.println("os.arch:" + System.getProperty("os.arch"));
		System.out.println("os.version:" + System.getProperty("os.version"));
		System.out.println("file.separator:" + System.getProperty("file.separator"));
		System.out.println("line.separator:" + System.getProperty("line.separator"));
		System.out.println("user.name:" + System.getProperty("user.name"));
		System.out.println("user.home:" + System.getProperty("user.home"));
		System.out.println("user.dir:" + System.getProperty("user.dir"));

	}

	public static void setJVMLibraryPath() {
		// "-Dserverjni=your lib dir"
		try {
			String serverjni = isWindows ? ";" : ":" + System.getProperty("serverjni");
			System.setProperty("java.library.path", System.getProperty("java.library.path") + serverjni);
			Field fieldSysPath = ClassLoader.class.getDeclaredField("sys_paths");
			fieldSysPath.setAccessible(true);
			fieldSysPath.set(null, null);
			// System.loadLibrary("JNIC");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
